package com.film.admincontroller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.film.domain.Film;
import com.film.service.DirectorService;
import com.film.service.FilmService;
import com.film.service.FilmTypeService;
import com.film.service.PerformerService;
import com.film.service.impl.DirectorServiceImpl;
import com.film.service.impl.FilmServiceImpl;
import com.film.service.impl.FilmTypeServiceImpl;
import com.film.service.impl.PerformerServiceImpl;
@WebServlet("/addFilm.do")
public class AddFIlmController extends  HttpServlet{
	private FilmService filmService = new FilmServiceImpl();
	private FilmTypeService filmTypeService = new FilmTypeServiceImpl();
	private DirectorService directorService = new DirectorServiceImpl();
	private PerformerService performerService = new PerformerServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
//		
		//文件上传
		//创建factory工厂
		DiskFileItemFactory factory = new DiskFileItemFactory();
		
		//servletFileUpload对象
		ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
		servletFileUpload.setHeaderEncoding("UTF-8");
		List<String> list = new ArrayList<>();
		String name = "";
		
		try {

			List<FileItem> items = servletFileUpload.parseRequest(request);
            //遍历
			for(FileItem item:items){
				//首先判断是普通文本项还是文件项，文件项与普通项由不同的API来处理
				if(item.isFormField()){
					//true表示普通文本项
					//获取文本项的name属性值
					//是普通的组件
					String value = item.getString("UTF-8");
					
					list.add(value);
				}else{
					//false表示文件项
					//获取文件名称
					name = item.getName();
					
					Long time = new Date().getTime();
					
					String realName ="img/film_img/"+time+""+name;
					String filmname = time+""+name;
					list.add(realName);
					//获取文件项的输入流
					InputStream in=item.getInputStream();
					//获取服务器端文件存储的目标磁盘路径
					String path=getServletContext().getRealPath("img/film_img");
					
					//获取输出流，输出到本地文件中
					File file = new File(path);
					if(!file.exists()) {
						file.mkdir();
						
					}
					if(!"".equals(name)) {
						File files = new File(path+"/"+filmname);
						OutputStream out=new FileOutputStream(path+"/"+filmname);
						
						//写入数据
						int len=0;
						byte[] b=new byte[1024];                    
						while((len=in.read(b))!=-1){
							out.write(b,0,len);
						}
						in.close();
						out.close();
					}
				}
			}
			
		} catch (FileUploadException e) {
			System.out.println("出错啦");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String showingDay = list.get(9);
		showingDay = showingDay+="中国大陆上映";
		Film film = new Film(list.get(1),list.get(2),list.get(3),list.get(10),showingDay,list.get(5),list.get(4),list.get(11));
		int result = filmService.addFilm(film,Integer.parseInt(list.get(0)));
		if(result != -1) {

	//		//添加电影类型中间表
			if(list.get(6)!=null && !"".equals(list.get(6))) {
				for(String tId:list.get(6).split("-")) {
					System.out.println("类型的添加:"+filmTypeService.addFilmType(result, Integer.parseInt(tId)));	
				}
			}
		
			
			//添加电影类导演中间表
			if(list.get(7)!=null && !"".equals(list.get(7))) {
				System.out.println("导演的添加:"+directorService.addFilmDirector(result, Integer.parseInt(list.get(7))));	
			}
			//添加电影演员中间表
			if(list.get(8)!=null && !"".equals(list.get(8))) {
				for(String pId:list.get(8).split("-")) {
					System.out.println("演员的添加:"+performerService.addFilmPerformer(result, Integer.parseInt(pId)));	
				}
			}
		}
		System.out.println("except:"+list.get(0));
		if(result == 1 || "0".equals(list.get(0))) {
			System.out.println("热门电影");
			response.sendRedirect("/film/adminHotFilm.do");
		}else if(result == 1 || "1".equals(list.get(0))) {
			System.out.println("即将上映电影");
			response.sendRedirect("/film/showFilmExhibition.do");
		}
		
		
	}
}
